package EventSystem

import (
	uuid "utils/uuid"

	. "git.oschina.net/yangdao/extlib/data_type"
)

type EventHandleFunc func(...interface{})

type EventHandleFuncData struct {
	Func EventHandleFunc
	UUID int64
}

type EventHandler struct {
	Name         string
	HandleFuncs  *TypVector
	ChildHandles *TypMap
}

func NewEventHandler(eventName string) *EventHandler {
	retHandler := &EventHandler{}
	retHandler.Name = eventName
	retHandler.ChildHandles = HashMap()
	retHandler.HandleFuncs = Vector()
	return retHandler
}

func (this *EventHandler) DispatchEvent(args ...interface{}) {

	for i := 0; i < this.HandleFuncs.Count(); i++ {

		this.HandleFuncs.Nth(i).(*EventHandleFuncData).Func(args...)
	}
	for _, v := range this.ChildHandles.Vals {
		v.(*EventHandler).DispatchEvent(args...)
	}
}

func (this *EventHandler) AddHandler(fn EventHandleFunc) *EventHandleFuncData {
	handlerData := &EventHandleFuncData{Func: fn, UUID: uuid.Int64UUID()}
	this.HandleFuncs.Conj(handlerData)
	return handlerData
}

func (this *EventHandler) RemoveHandler(fn *EventHandleFuncData) {
	this.HandleFuncs.Remove(fn)
}
